import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
const app = getApp()
Page(PAGE({
    data: {
        info: {},
        qrcodeNum: null,
        apiField: {
            module: 'charge',
            api: 'PayInfo',
            data: 'info',
            search: {
                DeviceId: null,
                Road: null
            }
        },
        showPayType: false,
        selectPayTypeIndex: 0, //索引0为微信支付
        selectTimeIndex: 0,
        payMoney: 0,
        timeList: []
    },
    async onLoad(options) {
        this.setData({
            'apiField.search.DeviceId': options[0],
            'apiField.search.Road': options[1]
        })


    },
    onShow() {
        this.fetchData()
    },
    async fetchData() {
        this.handleFetchData({
            complete: ($data) => {
                this.setData({
                    timeList: $data.PayTypeList[this.data.selectPayTypeIndex].TimeList
                })
                this.calc()
            },
            error: (e) => {
                // wx.navigateBack({
                //     delta: 1,
                // })

                wx.$Fun.showToast(e.frontCode)
            }
        }, 'apiField', true)
    },
    handleShowPayType(e) {
        this.setData({
            showPayType: true
        })
    },
    handlePayTypeChanged(e) {
        this.setData({
            selectPayTypeIndex: e.detail.value,
            selectTimeIndex: 0,
            timeList: this.data.info.PayTypeList[e.detail.value].TimeList,
            showPayType: false
        })

        this.calc()
    },
    handleTimeChanged(e) {
        this.setData({
            selectTimeIndex: e.currentTarget.dataset.index
        })
        this.calc()
    },
    calc() {
        var payType = this.data.info.PayTypeList[this.data.selectPayTypeIndex].Type
        if (payType == 1 || payType == 2) {
            var time = this.data.timeList[this.data.selectTimeIndex]
            this.setData({
                payMoney: time.Price
            })
        }
        else {
            this.setData({
                payMoney: 0
            })
        }
    },
    handlePay(e) {
        var payType = this.data.info.PayTypeList[this.data.selectPayTypeIndex].Type
        if (payType == 1) {
            //微信支付充电
            this.doPay()
        } else if (payType == 2) {
            //电卡支付
            this.powerCardPay()
        }
        else if (payType == 3) {
            //月卡充电
            this.monthPay()
        }
    },
    async doPay() {
        try {
            var req = {
                DeviceId: this.data.apiField.search.DeviceId,
                Road: this.data.apiField.search.Road,
                Money: this.data.payMoney,
            }
            const res = await wx.$Api.charge.TempChargePay({ data: req })

            //请求支付参数
            const prePayRes = await wx.$Api.payment.PrePay({ data: { tradeNo: res.Data } })
            wx.$Fun.wxPay(prePayRes.Data)
                .then(resp => {
                    wx.$Fun.toPage({
                        page: 'chargeList',
                        params: {
                            pageType: 'redirectTo'
                        }
                    })
                }).catch(e => {
                    console.log(e)
                })
        } catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
    async monthPay(e) {
        var _this = this
        wx.showModal({
            title: '提示',
            content: '确定使用月卡充电吗？',
            success: async function (showRes) {
                if (showRes.confirm) {
                    _this.doMonthPay()
                }
            }
        })
    },
    async doMonthPay() {
        try {
            var req = {
                DeviceId: this.data.apiField.search.DeviceId,
                Road: this.data.apiField.search.Road,
                CardId: this.data.info.PayTypeList[this.data.selectPayTypeIndex].CardId,
                Time: this.data.timeList[this.data.selectTimeIndex].Time
            }
            const res = await wx.$Api.charge.MonthCardCharge({ data: req })
            wx.$Fun.toPage({
                page: 'chargeList',
                params: {
                    pageType: 'redirectTo'
                }
            })

        } catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
    powerCardPay(e) {
        if (this.data.payMoney > this.data.info.PayTypeList[this.data.selectPayTypeIndex].Balance) {
            wx.$Fun.showToast('储值卡余额不足')
            return
        }
        var _this = this
        wx.showModal({
            title: '提示',
            content: '确定使用储值电卡支付充电吗？',
            success: async function (showRes) {
                if (showRes.confirm) {
                    _this.doPowerCardPay()
                }
            }
        })
    },
    async doPowerCardPay() {
        try {
            var req = {
                DeviceId: this.data.apiField.search.DeviceId,
                Road: this.data.apiField.search.Road,
                Money: this.data.payMoney,
                CardId: this.data.info.PayTypeList[this.data.selectPayTypeIndex].CardId
            }
            const res = await wx.$Api.charge.BalanceCharge({ data: req })
            wx.$Fun.toPage({
                page: 'chargeList',
                params: {
                    pageType: 'redirectTo'
                }
            })

        } catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    }
}))